<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\cpmjagent\controller\finance;

/**
 * Description of SettlementController
 *
 * @author SoChishun
 */
class WithdrawController extends \app\cpmjagent\controller\AppbaseController {

    public function withdraw() {
        $uid = $this->global_data['userid'];
        $banklist = \app\cpmjagent\model\finance\BankcardModel::all(['user_id' => $uid]);
        if (!$banklist) {
            $this->error('您未填写提现银行卡信息!', url('bankcardinfo'));
        }
        $blance = \app\cpmjagent\model\user\AgentModel::where(['id' => $uid])->value('blance');
        return view('', ['blance' => $blance, 'banklist' => $banklist]);
    }

    public function doWithdraw($amount = 0, $bankcardid = 0) {
        $uid = $this->global_data['userid'];
        $bankcarddata = \app\cpmjagent\model\finance\BankcardModel::get($bankcardid);
        $data = [
            'out_trade_no' => 'TX' . date('YmdHis'),
            'amount' => $amount,
            'bankname' => $bankcarddata['bankname'],
            'bankcardholder' => $bankcarddata['bankcardholder'],
            'bankcardno' => $bankcarddata['bankcardno'],
            'user_id' => $uid,
            'remark' => '',
            'status' => 0,
        ];
        \app\cpmjagent\model\finance\WithdrawModel::create($data);
        $where = ['id' => $uid];
        \app\cpmjagent\model\user\AgentModel::where($where)->setDec('blance', $amount);
        \app\cpmjagent\model\user\AgentModel::where($where)->setInc('blance_frozen', $amount);
        $this->success('提现申请成功', url('withdrawlist'));
    }

    public function withdrawlist() {
        $uid = $this->global_data['userid'];
        $where = [];
        if (!$this->global_data['isadmin']) {
            $where['user_id'] = $uid;
        }
        $list = \app\cpmjagent\model\finance\WithdrawModel::where($where)->paginate(10);
        return view('', ['list' => $list]);
    }

    public function bankcardinfo() {
        $uid = $this->global_data['userid'];
        $bankcarddata = \app\cpmjagent\model\finance\BankcardModel::get(['user_id' => $uid, 'account_type' => 1]);
        $alipaydata = \app\cpmjagent\model\finance\BankcardModel::get(['user_id' => $uid, 'account_type' => 2]);
        return view('', ['alipaydata' => $alipaydata, 'bankcarddata' => $bankcarddata]);
    }

    public function saveBankcardInfo($zhifubao = '', $bankname = '', $bankcardholder = '', $bankcardno = '', $alipayid = 0, $bankcardid = 0) {
        if ($zhifubao && !xsfn_validate_string($zhifubao, 6, 32)) {
            $this->error('支付宝无效', url('bankcardinfo'), '', 2);
        }
        if (!xsfn_validate_string($bankname, 6, 32)) {
            $this->error('开户行无效', url('bankcardinfo'), '', 2);
        }
        if (!xsfn_validate_string($bankcardholder, 2, 32)) {
            $this->error('持卡人无效', url('bankcardinfo'), '', 2);
        }
        if (!xsfn_validate_string($bankcardno, 6, 32)) {
            $this->error('卡号无效', url('bankcardinfo'), '', 2);
        }
        $uid = $this->global_data['userid'];
        $mBankcard = new \app\cpmjagent\model\finance\BankcardModel();
        if ($zhifubao) {
            $alipaydata['bankcardno'] = $zhifubao;
            $alipaydata['account_type'] = 2; // 1:银行卡;2:支付宝;3:微信
            $alipaydata['user_id'] = $uid;
            $alipaydata['title'] = '支付宝:' . substr($zhifubao, 0, 3) . '...' . substr($zhifubao, strlen($zhifubao) - 3);
            if ($alipayid) {
                $mBankcard->save($alipaydata, ['id' => $alipayid]);
            } else {
                $mBankcard->save($alipaydata);
            }
        }
        $data['bankname'] = $bankname;
        $data['bankcardholder'] = $bankcardholder;
        $data['bankcardno'] = $bankcardno;
        $data['account_type'] = 1;
        $data['user_id'] = $uid;
        $data['title'] = substr($bankcardno, 0, 3) . '...' . substr($bankcardno, strlen($bankcardno) - 3);
        if ($bankcardid) {
            $mBankcard->save($data, ['id' => $bankcardid]);
        } else {
            $mBankcard->save($data);
        }
        $this->success('保存成功', url('withdraw'));
    }

}
